package service.impl;


import dao.SoilMapper;
import org.springframework.stereotype.Service;
import service.SoilService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


@Service("soilService")
public class SoilServiceImpl implements SoilService {
    @Resource(name = "soilMapper")
    private SoilMapper soilMapper;

    /**
     * 将数据库中查询到的深度范围解析成数组形式
     *
     * @param str 查询到的深度范围
     * @return
     */
    public double[] depthParse(String str) {
        try {
            //去除空格
            str = str.replace(" ", "");
            int length = str.length();
            //记录分割两数的下标
            int indexOfNotNumber = -1;
            //定义分割点的左右字符串
            StringBuilder left = new StringBuilder();
            StringBuilder right = new StringBuilder();
            //深度最小值和最大值
            double min, max;

            //查找分割点
            for (int i = 0; i < length; i++) {
                char c = str.charAt(i);
                if (c < '0' || c > '9') {
                    indexOfNotNumber = i;
                    break;
                }
            }
            //存在分割点则解析分割点左右两侧的字符并返回
            if (indexOfNotNumber != -1) {
                //拼接左侧字符串
                for (int i = 0; i < indexOfNotNumber; i++) {
                    left.append(str.charAt(i));
                }
                //拼接右侧字符串
                for (int i = indexOfNotNumber + 1; i < length; i++) {
                    right.append(str.charAt(i));
                }
                min = Double.parseDouble(left.toString());
                max = Double.parseDouble(right.toString());
                //如果最小值比最大值还大，则交换
                if (min > max) {
                    double temp = min;
                    min = max;
                    max = temp;
                }
                return new double[]{min, max};
            } else {
                return null;
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 通过城市名称查询数据
     *
     * @param city 城市名称
     * @return
     */
    @Override
    public List<Map<String, Object>> getDataByCityName(String city) {
        return soilMapper.getDataByCityName(city);
    }

    /**
     * 通过经纬度查询数据
     *
     * @param longitude 经度
     * @param latitude  纬度
     * @return
     */
    @Override
    public List<Map<String, Object>> getDataByCoordinate(String longitude, String latitude) {
        return soilMapper.getDataByCoordinate(longitude, latitude);
    }

    /**
     * 通过经纬度和深度查询
     *
     * @param longitude 经度
     * @param latitude  纬度
     * @param depth     深度
     * @return
     */
    @Override
    public List<Map<String, Object>> getDataByCoordinateAndDepth(String longitude, String latitude, Double depth) {
        List<Map<String, Object>> resultList = soilMapper.getDataByCoordinate(longitude, latitude);
        for (int i = 0; i < resultList.size(); i++) {
            Map<String, Object> map = resultList.get(i);
            String depth_result = (String) map.get("N004-发生层深度-sxd");
            double[] depthRange = depthParse(depth_result);//获取范围
            if (depthRange != null) {
                if (depth < depthRange[0] || depth > depthRange[1]) {
                    resultList.remove(map);
                    i--;//移除后i要回溯
                }
            }
        }
        return resultList;
    }

    /**
     * 通过城市名称查询采样点数量
     *
     * @param city
     * @return 采样点数量
     */
    @Override
    public int countPointByCityName(String city) {
        return soilMapper.countPointByCityName(city);
    }

    /**
     * 获取各城市采样点数量映射
     *
     * @param originData 原始数据
     * @return 各城市采样点数量映射
     */
    @Override
    public Map<String, Object> getGlobalData(List<Map<String, Object>> originData) {
        //城市采样点数量映射
        Map<String, Object> countMap = new HashMap<>();
        //采样点总数
        int totalPoint = soilMapper.getNumberOfPoint();

        //遍历原始数据
        for (Map<String, Object> map : originData) {
            Set<String> keySet = map.keySet();
            for (String key : keySet) {
                //获取城市名称
                String cityName = (String) map.get(key);
                //获取该城市的采样点数量
                int count = countPointByCityName(cityName);
                //添加到城市采样点数量map中
                countMap.put(cityName, count);
            }
        }
        //添加采样点总数到城市采样点数量map中
        countMap.put("scatter", totalPoint);
        return countMap;
    }

    /**
     * 获取所有散点的值
     *
     * @return 每一个散点的名称和对应的经纬度
     */
    @Override
    public Map<String, String[]> getAllPoint() {
        //获取所有的采样点经纬度
        List<Map<String, Object>> allPoint = soilMapper.getAllPoint();
        //创建一个保存散点经纬度信息的map
        HashMap<String, String[]> pointData = new HashMap<>();
        int count = 0;
        for (Map<String, Object> map : allPoint) {
            String longitude = (String) map.get("N009-经度");//经度
            String latitude = (String) map.get("N010-纬度");//纬度
            pointData.put("采样点" + (++count), new String[]{longitude,latitude});
        }
        return pointData;
    }
}
